'use strict'

const path = require('path');

const { TaiSpider, ItemLoader } = require('tai-spider');
const { saveJsonFile } = require('../utils/file-helper');
const { formatDate } = require('../utils/str-helper');

const keywords = [
    'space surveillance',
    'space awareness',
    'space policy',
    'space strategy',
    'space threat',
    'space program',
    'satellite launch',
    'missile launch',
    'rocket launch',
    'satellite image',
    'satellite enter service',
    'countspace',
    'space debris',
    'ballistic missile',
    'missile defense',
    'missile warning',
    'anti-ballistic missile defense',
    'orbital maneuver',
    'approach',
    'rendezvous',
    'malfunction',
    'reentry',
    'decay',
    'nuclear explosion',
    'break up',
];

class SpacenewsSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'spacenews';
        this.debug = true;
        this.start_urls = () => {
            let list = [];
            Array(4)
            .fill(0)
            .map((i, idx) => {
                keywords.map((keyword) => {
                    if (idx === 0)
                        list.push(`https://spacenews.com/?s=${encodeURIComponent(keyword)}&orderby=date-desc`);
                    else
                        list.push(`https://spacenews.com/page/${idx + 1}/?s=${encodeURIComponent(keyword)}&orderby=date-desc`);
                  })
            });
            return list;
        };
        // this.start_urls = [
        //     'https://spacenews.com/event/2023-beyond-earth-symposium-leo-to-lunar-to-living-beyond-earth-policy-pathways-to-space-migration/'
        // ];
    }

    *parse(response) {
        // response.saveHtml();
        for (let ele of response.css('.component-listing-card')) {
            // yield {
            //     'title': ele.css('.entry-title > a').extract_first(),
            //     'link': ele.css('.entry-title > a').get(0).attr('href'),
            //     'author': ele.css('.author > a').extract_first(),
            //     'pubdate': ele.css('.published').get(0).attr('datetime'),
            //     'excerpt': ele.css('.entry-content').extract_first(),
            //     'tags': ele.css('.cat-links > a').extract(),
            // };
            yield* response.follow_all(ele.css('.entry-title > a'), this.parseArticle, {
                exclude: (link) => {
                    return /\/event\//.test(link);
                }
            });
        }
    }

    *parseArticle(response, spider) {
        // const { store } = spider.options;
        let data = response.extract();
        let date = formatDate(data.date)
        data.timestamp = date.valueOf();
        data.date = date.format();
        // yield saveJsonFile(path.join(store.path, data.title + '.json'), data)

        yield Object.assign({
            index_name: 'tai-article',
        }, data);
    }
}

module.exports = SpacenewsSpider;